package org.metaverse.h2.test;

import java.sql.Connection;

import org.h2.jdbcx.JdbcConnectionPool;
import org.metaverse.h2.sql.SQL;
/**
 * 
 * @author vjache
 *
 */
public class ConnectionPoolHelper {
	private static SQL _sql;
		
	public synchronized static SQL getSql()
	{
		if(_sql==null)
			try {
				_sql = new SQL("PUBLIC");
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		return _sql;
	}
	
	public static JdbcConnectionPool createPool() throws Exception
	{		
		return createPool("jdbc:h2:~/_db/mvwh-test");
	}
	public static JdbcConnectionPool createPool(String aUrl) throws Exception
	{
		JdbcConnectionPool connPool = JdbcConnectionPool.create(
				aUrl, "sa", "sa");
		connPool.setMaxConnections(64);
		
		final Connection conn = connPool.getConnection();
		final java.sql.Statement create = conn.createStatement();
		create.execute(getSql().getDDL());
		create.close();
		
		final java.sql.Statement clean = conn.createStatement();
		clean.execute(getSql().getWarehouseClean());
		clean.close();
		conn.close();
		
		return connPool;
	}

}
